package com.ml.common.gateway.service;

import com.ml.common.gateway.utils.RedisClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;

import java.util.Set;

@Service
public class RankingService {

    private static final String RANKGNAME="user_score";
    @Autowired
    private RedisClient redisService;

    /**
     * 初始化给uid添加分数
     * @param uid
     * @param score
     */
    public void rankAdd(String uid, Integer score) {
        redisService.zAdd(RANKGNAME, uid, score);
    }

    /**
     * 对某个uid新增分数
     * @param uid
     * @param score
     */
    public void increSocre(String uid, Integer score) {

        redisService.incrementScore(RANKGNAME, uid, score);
    }

    /**
     * 获取uid的排名，是从第0名开始的
     * @param uid
     * @return
     */
    public Long rankNum(String uid) {
        return redisService.zRank(RANKGNAME, uid);
    }

    /**
     * 获取uid的具体分数
     * @param uid
     * @return
     */
    public Long score(String uid) {
        Long score = redisService.zSetScore(RANKGNAME, uid).longValue();
        return score;
    }

    /**
     * 获取从小到大的排行，end为-1表示到结束
     * @param start
     * @param end
     * @return
     */
    public Set<ZSetOperations.TypedTuple<Object>> rankWithScore(Integer start, Integer end) {
        return redisService.zRankWithScore(RANKGNAME, start, end);
    }
}
